$(document).ready(function(){
	$("#jqwCloseBtn").on("click", function(){
		var jqw = new jqWindow();
		jqw.destroyJqwSelected();
	});
	
	$("#jqwFormReviseSaveBtn").on("click", function(){
		if(!validationForm({isUid:false, isPwd:false})) return;

		var chkFields = new Object();
		chkFields["actionType"] = "revise";
		chkFields["usrId"] = $("#usrId").val();
		checkDuplication.existUsrInfoId(chkFields, dwrResult);
	});
	
	$("#jqwFormSaveBtn").on("click", function(){
		if(!validationForm({isUid:true, isPwd:true})) return;

		var chkFields = new Object();
		chkFields["actionType"] = "new";
		chkFields["usrId"] = $("#usrId").val();
		checkDuplication.existUsrInfoId(chkFields, dwrResult);
	});
	
	
	$("#groupLst").on("dblclick", function(){
		$("#groupLst option:selected").each(function(idx, item){
			appendComBox($(item), $("#usrGroupLst"));
		});
	});
	
	$("#usrGroupLst").on("dblclick", function(){
		$("#usrGroupLst option:selected").each(function(idx, item){
			appendComBox($(item), $("#groupLst"));
		});
	});
	
	$("#usrGroupUpArrow").on("click", {direction:"up"}, actionUpDownArrow);
	$("#usrGroupDwArrow").on("click", {direction:"down"}, actionUpDownArrow);
});

function actionUpDownArrow(event){
	var obj = $(".usrInfoContsTd #usrGroupLst");
	var currObj = $(obj).children("option:selected");
	var currObjIdx = $(currObj).index();
	
	if($(currObj).length <= 0) {
		alert("위치 변경할 그룹을 선택하지 않았습니다.");
		return;		
	}
	
	if(event.data.direction == "up"){
		if(currObjIdx <= 0){
			alert("더 이상 위치 변경을 할 수 없습니다.");
			return;
		}
		
		var prevObj = $(obj).children("option").eq(currObjIdx - 1);
		$(prevObj).before($(currObj));
		
	}else{
		if(currObjIdx >= ($(obj).children("option").length - 1)){
			alert("더 이상 위치 변경을 할 수 없습니다.");
			return;
		}
		
		var nextObj = $(obj).children("option").eq(currObjIdx + 1);
		$(nextObj).after($(currObj));
	}
}

function appendComBox(src, tar){
	var obj = $(src).clone();
	$(src).remove();
	$(tar).append($(obj));
}

function dwrResult(data){
	var isMe = $("input[name='isMe']");
	var jHelper = new jsonHelper2();
	var json = jHelper.stringToJsonObj(data);

	if(!json.result){
		var url = "/template/system/userInfo/"+((json.actionType == "new")?"_regFormSave":"_regFormReviseSave");
		var options = new Object();
		options["url"] = url;
		if($(isMe).length > 0 && $(isMe).val() == "true"){
			options["jqwType"] = "close";
			
		}else{
			options["jqwType"] = "save";
		}
		
		jqwSave(options);
		
	}else{
		alert(json.msg);
		$("#"+json.chkField).focus();
		return;
	}
}

function validationForm(data){
	var def = new validDefinition(data);
	var validator = new formValidator("frmUserInfoReg", def.rules, def.messages);
	return validator.validation();
}

function jqwSave(opts){
	var obj = $(".pageNavi").find(".numberingOn");
	if($(obj).length > 0) naviNumClickEventReg(obj);
	
	$("#usrGroupLst option").each(function(idx, item){
		if($(item).is(":disabled")) $(item).attr("disabled", false);
		$(item).attr("selected", true);
	});
	
	var options = new Object();
	options["url"] = opts["url"];
	options["type"] = "POST";
	options["dataType"] = "json";
	options["data"] = "frm:frmUserInfoReg";	//Available no writing this option OR pram1=?&param2=? OR frm:form Id
	options["triggerObj"] = obj;	//트리 메뉴에서 현재 화면을 triggering(to click) 할 수 있는 오브젝트.
	options["jqwType"] = opts["jqwType"];	//< "popup" | "save" | "close" ] >
	
	var jqw = new jqWindow();
	jqw.jqwSet(options);
	jqw.loadJqWindow();
}
